package com.jy.shenghuilogistics.controller;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.jy.shenghuilogistics.bean.Dispatchinfo;
import com.jy.shenghuilogistics.bean.Document;
import com.jy.shenghuilogistics.bean.DouDispatch;
import com.jy.shenghuilogistics.bean.Trucks;
import com.jy.shenghuilogistics.service.DocumentService;
import com.jy.shenghuilogistics.service.TruckService;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class TruckController {
    @Resource
    TruckService truckService;
    @Resource
    DocumentService documentService;

    //查询车辆最新一次调度所有携带的运单
    @RequestMapping("getDocByDispa")
    public List<DouDispatch> getDocByDispa(Integer id) {
        return truckService.getDocByDispa(id);
    }
    //询站点所有待接收货车
    @RequestMapping("getWaitTruck")
    public List<Trucks> getWaitTruckBySite(String name) {
        return truckService.getWaitTruckBySite(name);
    }
    //装载发车
    @RequestMapping("truckDepart")
    @Transactional
    public String truckDepart(Trucks t,@RequestParam(value = "documents",required=false) List<Integer> documents) {
        Integer result = 0;
        result += truckService.updTruck(t);//修改车辆状态
        Dispatchinfo d = new Dispatchinfo();
        d.setTruckId(t.getTruckId());
        d.setNowSite(t.getNowSite());
        d.setEndSite(t.getEndSite());
        d.setDisTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Integer dispatchId = truckService.addDispa(d);   //添加调度信息
        result += truckService.addDocDispa(documents,dispatchId);
        if("中转中".equals(t.getTruckState())) {
            for(Integer docId : documents) {
                documentService.addLoginInfo(docId,t.getNowSite(),t.getEndSite(),"已发出");
            }
        }else if("配送中".equals(t.getTruckState())){
            for(Integer docId : documents) {
                documentService.addLoginInfo(docId,t.getNowSite(),"配送中");
                Document doc = new Document();
                doc.setDocId(docId);
                doc.setState("配送中");
                documentService.updateDocu(doc);
            }
        }
        return result.toString();
    }
    //车辆数据修改
    @RequestMapping("updTruck")
    public String updTruck(Trucks t) {
        return truckService.updTruck(t).toString();
    }
    //车辆模糊查询
    @RequestMapping("getTruck")
    public List<Trucks> getTruck(Trucks t) {
        return truckService.getTruck(t);
    }

    @PostMapping("addTruck")
    public String addTruck(Trucks t) {
        return truckService.addTruck(t);
    }

    @RequestMapping("selDispatch")
    public List<Dispatchinfo> selDispatch(Dispatchinfo d) {
        return truckService.selDispatch(d);
    }

    @RequestMapping("getDocByDisId")
    public List<DouDispatch> getDocByDisId(Integer disId) {
        return truckService.getDocByDisId(disId);
    }
}
